﻿#include "SMTDataBindingTools.h"
#include<QLineEdit>

SMTDataBindingTools::SMTDataBindingTools()
{
}


void SMTDataBindingTools::InitUIControl(SMTOperator *m_opr, int ParamsIndex, QSpinBox *pWidget)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		int value;
		val.toInt(&value);

		pWidget->blockSignals(true);
		pWidget->setValue(value);
		pWidget->blockSignals(false);
	}
}

void SMTDataBindingTools::InitUIControl(SMTOperator *m_opr, int ParamsIndex, QDoubleSpinBox *pWidget)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		float value;
		val.toFloat(&value);

		pWidget->blockSignals(true);
		pWidget->setValue(value);
		pWidget->blockSignals(false);
	}
}

void SMTDataBindingTools::InitUIControl(SMTOperator *m_opr, int ParamsIndex, QLineEdit *pWidget)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		SMTString value;
		val.toString(&value);

		pWidget->blockSignals(true);
		pWidget->setText(value.toLocal8Bit().constData());
		pWidget->blockSignals(false);
	}
}

void SMTDataBindingTools::InitUIControl(SMTOperator *m_opr, int ParamsIndex, QComboBox *pWidget)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		SMTNumber number;
		val.toNumber(&number);

		pWidget->blockSignals(true);
		pWidget->setCurrentIndex(number.toInt());
		pWidget->blockSignals(false);
	}
}

void SMTDataBindingTools::InitUIControl(SMTOperator *m_opr, int ParamsIndex, SMTVariableArray *pVariableArray, SMTVariable::VariableType type, QComboBox *pWidget)
{
	SMTVariableArray *inParams;
	QString DisplayName;
	if (m_opr)
	{
		SMTVariable variable;
		SMTRefFormat refFormat = SMTRefFormat::SToS();
		QString ValueName;
		m_opr->GetInputVariables(&inParams);
		bool bFalg = (*inParams)[ParamsIndex].getReferenceInfo(0, variable, &refFormat);
		if (bFalg == true)
		{
			//ValueName = variable.parent()->secName().toLocal8Bit().constData() + '-';
			//ValueName += variable.parent()->name().toLocal8Bit().constData() + '.';
			ValueName += variable.name().toLocal8Bit().constData();
		}

		for (int i = 0; i < pVariableArray->length(); i++)
		{
			SMTVariable variable;
			pVariableArray->select(i, &variable);
			if (!variable.valid() || !variable.enabled() || !variable.isInUse())
			{
				continue;
			}
			SMTOperator* opr = variable.parent();
			QString strname;
			if (opr)
			{
				strname = opr->secName().toLocal8Bit().constData();
				strname = strname + "-" + opr->name().toLocal8Bit().constData() + ".";
			}
			else
			{
				strname = QString("Variant") + ".";
			}

			if (variable.variableType() == type)
			{
				QString itemname = strname + variable.name().toLocal8Bit().constData();
				pWidget->addItem(itemname);
				if (variable.name().toLocal8Bit().constData() == ValueName)
				{
					DisplayName = itemname;
				}
			}
		}

		if (!DisplayName.isEmpty())
		{
			pWidget->blockSignals(true);
			pWidget->setCurrentText(DisplayName);
			pWidget->blockSignals(false);
		}
	}

}

void SMTDataBindingTools::InitUIControl(SMTOperator *m_opr, int ParamsIndex, SMTSwitchButton *pWidget)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		bool value;
		val.toBool(&value);

		pWidget->blockSignals(true);
		pWidget->setChecked(value);
		pWidget->blockSignals(false);
	}
}

void SMTDataBindingTools::InitUIControl(SMTOperator *m_opr, int ParamsIndex, SMTSpinBox *pWidget)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		int value;
		val.toInt(&value);

		pWidget->blockSignals(true);
		pWidget->setValue(value);
		pWidget->blockSignals(false);
	}
}

void SMTDataBindingTools::InitUIControl(SMTOperator *m_opr, int ParamsIndex, SMTButtonBox *pWidget)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		int value;
		val.toInt(&value);

		pWidget->blockSignals(true);
		pWidget->setCurrentIndex(value);
		pWidget->blockSignals(false);
	}
}

void SMTDataBindingTools::InitROIAlignment(SMTOperator *m_opr, SMTVariableArray *pVariableArray, QComboBox *pWidget)
{
	SMTOperatorROI *m_roi;

	m_opr->getROI(&m_roi);
	SMTVariable m_RoiAlinReference;
	m_roi->getROIAlignReference(&m_RoiAlinReference);
	SMTOperator* opr = m_RoiAlinReference.parent();
	QString ROIAlignName;
	if (opr)
	{
		ROIAlignName = opr->secName().toUtf8().constData();
		ROIAlignName = ROIAlignName + "-" + opr->name().toUtf8().constData() + ".";
	}
	ROIAlignName = ROIAlignName + m_RoiAlinReference.name().toUtf8().constData();

	if (m_opr)
	{
		for (int i = 0; i < pVariableArray->length(); i++)
		{
			SMTVariable variable;
			pVariableArray->select(i, &variable);
			if (!variable.valid() || !variable.enabled() || !variable.isInUse())
			{
				continue;
			}
			SMTOperator* opr = variable.parent();
			QString strname;
			if (opr)
			{
				strname = opr->secName().toLocal8Bit().constData();
				strname = strname + "-" + opr->name().toLocal8Bit().constData() + ".";
			}
			else
			{
				strname = QString("Variant") + ".";
			}

			if (variable.variableType() == SMTVariable::VariableType::VARIABLE_ROIALIGNMENT)
			{
				QString itemname = strname + variable.name().toLocal8Bit().constData();
				pWidget->addItem(itemname);
				if (variable.name().toLocal8Bit().constData() == strname)
				{
					ROIAlignName = itemname;
				}
			}
		}
	}

	if (!ROIAlignName.isEmpty())
	{
		pWidget->blockSignals(true);
		pWidget->setCurrentText(ROIAlignName);
		pWidget->blockSignals(false);
	}
}

void SMTDataBindingTools::GetNumberParams(SMTOperator *m_opr, int ParamsIndex, SMTNumber *value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		val.toNumber(value);
	}

}

void SMTDataBindingTools::SetNumberParams(SMTOperator *m_opr, int ParamsIndex, SMTNumber value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		val.fromNumber(value);
		bool test = (*inParams)[ParamsIndex].setValue(val);
	}
}

void SMTDataBindingTools::GetStirngParams(SMTOperator *m_opr, int ParamsIndex, SMTString *value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		val.toString(value);
	}
}

void SMTDataBindingTools::SetStringParams(SMTOperator *m_opr, int ParamsIndex, SMTString value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		val.fromString(value);
		(*inParams)[ParamsIndex].setValue(val);
	}
}

void SMTDataBindingTools::GetBoolParamas(SMTOperator *m_opr, int ParamsIndex, SMTBool *value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		val.toBool(value);
	}
}

void SMTDataBindingTools::SetBoolParamas(SMTOperator *m_opr, int ParamsIndex, SMTBool value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		val.fromBool(value);
		bool check = (*inParams)[ParamsIndex].setValue(val);
	}
}

void SMTDataBindingTools::GetPointParams(SMTOperator *m_opr, int ParamsIndex, SMTPoint *value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		val.toPoint(value);
	}
}

void SMTDataBindingTools::SetPointParams(SMTOperator *m_opr, int ParamsIndex, SMTPoint value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		val.fromPoint(value);
		(*inParams)[ParamsIndex].setValue(val);
	}
}

void SMTDataBindingTools::GetNumberArrayParams(SMTOperator *m_opr, int ParamsIndex, SMTNumberArray *value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		val.toNumberArray(value);
	}
}

void SMTDataBindingTools::SetNumberArrayParams(SMTOperator *m_opr, int ParamsIndex, SMTNumberArray value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		val.fromNumberArray(value);
		(*inParams)[ParamsIndex].setValue(val);
	}
}

void SMTDataBindingTools::GetStringArrayParams(SMTOperator *m_opr, int ParamsIndex, SMTStringArray *value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		val.toStringArray(value);
	}
}

void SMTDataBindingTools::SetStringArrayParams(SMTOperator *m_opr, int ParamsIndex, SMTStringArray value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		val.fromStringArray(value);
		(*inParams)[ParamsIndex].setValue(val);
	}
}

void SMTDataBindingTools::GetBoolArrayParams(SMTOperator *m_opr, int ParamsIndex, SMTBoolArray *value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		val.toBoolArray(value);
	}
}

void SMTDataBindingTools::SetBoolArrayParams(SMTOperator *m_opr, int ParamsIndex, SMTBoolArray value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		val.fromBoolArray(value);
		(*inParams)[ParamsIndex].setValue(val);
	}
}

void SMTDataBindingTools::GetPointArrayParams(SMTOperator *m_opr, int ParamsIndex, SMTPointArray *value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		val.toPointArray(value);
	}
}

void SMTDataBindingTools::SetPointArrayParams(SMTOperator *m_opr, int ParamsIndex, SMTPointArray value)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		val.fromPointArray(value);
		(*inParams)[ParamsIndex].setValue(val);

	}
}

void SMTDataBindingTools::GetShapeArrayParams(SMTOperator *m_opr, int ParamsIndex, SMTShapeArray *shapeArray)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		(*inParams)[ParamsIndex].getValue(&val);
		val.toShapeArray(shapeArray);
	}
}

void SMTDataBindingTools::SetShapeArrayParams(SMTOperator *m_opr, int ParamsIndex, SMTShapeArray shapeArray)
{
	SMTVariableArray *inParams;
	if (m_opr)
	{
		SMTVariant val;
		m_opr->GetInputVariables(&inParams);
		val.fromShapeArray(shapeArray);
		(*inParams)[ParamsIndex].setValue(val);
	}
}

void SMTDataBindingTools::SetReferenceParams(SMTOperator *m_opr, int ParamsIndex, SMTVariableArray *pVariableArray, QString ParamsName, const SMTRefFormat &format)
{
	SMTVariableArray *inParams;
	m_opr->GetInputVariables(&inParams);
	if (m_opr)
	{
		for (int i = 0; i < pVariableArray->length(); i++)
		{
			SMTVariable variable;
			pVariableArray->select(i, &variable);

			SMTOperator* opr = variable.parent();
			QString strname;
			if (opr)
			{
				strname = opr->secName().toUtf8().constData();
				strname = strname + "-" + opr->name().toUtf8().constData() + ".";
			}
			else
			{
				strname = QString("Variant") + ".";
			}

			strname = strname + variable.name().toUtf8().constData();

			if (ParamsName == strname)
			{
				SMTVariant val;
				variable.getValue(&val);
				(*inParams)[ParamsIndex].clearReference();
				bool resutl = (*inParams)[ParamsIndex].addReference(variable, format);
				(*inParams)[ParamsIndex].setUseReference(true);
				break;
			}
		}
	}
}

SMTDataBindingTools::~SMTDataBindingTools()
{

}
